 package com.itbour.web.action.trade;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.google.gson.Gson;
import com.itbour.com.page.Page;
import com.itbour.common.base.BaseAction;
import com.itbour.common.util.StringUtils;
import com.itbour.dto.com.UserInfoDto;
import com.itbour.dto.trade.ItemInfoDto;
import com.itbour.dto.trade.PriceInfoDto;
import com.itbour.service.trade.ItemsService;
import com.itbour.service.trade.PriceService;
import com.opensymphony.xwork2.ActionContext;

/**
 * copyright Itbour team 2016
 * 
 * @author abby
 */

public class GetPriceWebAction extends BaseAction {

	private static final long serialVersionUID = 1L;

	private PriceService priceService;
	private String delIds;
	
	// [商品]
	private HashMap<String, Object> priceMapResult = new HashMap<String, Object>();//[分页]商品信息列表
	private List<PriceInfoDto> priceInfoList;			// 商品信息列表
	private PriceInfoDto priceInfo;						// 更新用
	private String priceId;								// 商品id
	private String findJgState;							//检索条件[价格状态]
	private String findJgShow;							//检索条件[数量显示值]
	
	private String findPubFlg;// 检索条件[发布状态]
	private String ids;// 通用ids[逗号连接]
	private String order;// 显示顺序
	
	
	// 分页获取商品信息
	public String getPriceInfoPg() throws Exception {
		// 用户信息
		Map<String, Object> session = ActionContext.getContext().getSession();
    	UserInfoDto user = (UserInfoDto)session.get("user");
    	
		HttpServletRequest request = ServletActionContext.getRequest();
		String pageNumber = request.getParameter("page");
		String pageSize = request.getParameter("rows");
		
		Map<String, Object> param = new HashMap<String, Object>();
		if (StringUtils.isNotEmpty(findJgShow)) {
			param.put("show", "%"+findJgShow+"%");
		}
		if (StringUtils.isNotEmpty(findJgState)) {
			param.put("state", findJgState);
		}
		Page page = priceService.pricePgQuery(param,
				Integer.parseInt(pageNumber),
				Integer.parseInt(pageSize));
		HashMap<String, Object> resMap = new HashMap<String, Object>();
		resMap.put("total", page.getTotalCount());
		resMap.put("rows", page.getResult());
		//writeJSON(resMap);
		priceMapResult = resMap;
		return SUCCESS;
	}
	
	// 获取商品信息列表
	public String getPriceInfoLst() throws Exception {
		HttpServletRequest request = ServletActionContext.getRequest();
		Map<String, Object> param = new HashMap<String, Object>();
		String id = request.getParameter("price_id");
		if (StringUtils.isNotEmpty(id)) {
			param.put("price_id", id);
		}
		priceInfoList = priceService.selectPriceInfo(param);
		return SUCCESS;
	}
	
	// 更新价格信息列表
	public void updPrice() throws Exception {
		if (priceInfo != null && 
			((priceInfo.getPrice_id() != null && priceInfo.getPrice_id()>0))) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	
	    	Map<String, Object> param = new HashMap<String, Object>();
	    	
	    	if(priceInfo.getPrice_id() != null && priceInfo.getPrice_id()>0){
		    	param.put("price_id", priceInfo.getPrice_id());
	    	}	 
	    	if(user != null) {
	    		param.put("update_by", user.getUser_id());
	    	}
	    	if(priceInfo.getState() != null) {
	    		param.put("state", priceInfo.getState());
	    	}
	    	if(priceInfo.getPrice() != null ){
		    	param.put("price", priceInfo.getPrice());
	    	}
	    	if(priceInfo.getUnit() != null) {
	    		param.put("unit", priceInfo.getUnit());
	    	}
	    	if(priceInfo.getMax() != null) {
	    		param.put("max", priceInfo.getMax());
	    	}
	    	if(priceInfo.getMin() != null) {
	    		param.put("min", priceInfo.getMin());
	    	}
	    	if(priceInfo.getShow() != null) {
	    		param.put("show", priceInfo.getShow());
	    	}
	    	if(priceInfo.getDesc() != null) {
	    		param.put("desc", priceInfo.getDesc());
	    	}
	    	priceService.updPrice(param);
		}
		this.writeJSON("ok");
	}	
	
	// 添加商品信息
	/*public void addPrice() throws Exception {
		if (priceInfo != null) {
			Map<String, Object> session = ActionContext.getContext().getSession();
	    	UserInfoDto user = (UserInfoDto)session.get("user");
	    	if (user != null) {
	    		priceInfo.setUpdate_by(user.getUser_id().toString());
	    	}
	    	
	    	if(priceInfo.getPp_id()!=null && priceInfo.getPp_id()>0){
	    		// 更新
	    		this.updPrice();
	    	} else {
	    		// 新建
	    		priceInfo.setState(10);// 编辑中
	    		priceService.addPrice(priceInfo);
	    		this.writeJSON("ok");
	    	}
		} 
		else {
			this.writeJSON("ok");
		}
	}*/
		
	//删除信息列表
	public void delPrice() throws Exception {
		priceService.delPrice(ids);
		this.writeJSON("ok");
	}
	
	
	public PriceService getPriceService() {
		return priceService;
	}

	public void setPriceService(PriceService priceService) {
		this.priceService = priceService;
	}

	public HashMap<String, Object> getPriceMapResult() {
		return priceMapResult;
	}

	public void setPriceMapResult(HashMap<String, Object> priceMapResult) {
		this.priceMapResult = priceMapResult;
	}

	public List<PriceInfoDto> getPriceInfoList() {
		return priceInfoList;
	}

	public void setPriceInfoList(List<PriceInfoDto> PriceInfoList) {
		this.priceInfoList = priceInfoList;
	}

	public PriceInfoDto getPriceInfo() {
		return priceInfo;
	}

	public void setPriceInfo(PriceInfoDto priceInfo) {
		this.priceInfo = priceInfo;
	}

	public String getPriceId() {
		return priceId;
	}

	public void setPriceId(String priceId) {
		this.priceId = priceId;
	}

	public String getFindPubFlg() {
		return findPubFlg;
	}

	public void setFindPubFlg(String findPubFlg) {
		this.findPubFlg = findPubFlg;
	}

	public String getIds() {
		return ids;
	}

	public void setIds(String ids) {
		this.ids = ids;
	}

	public String getFindJgState() {
		return findJgState;
	}

	public void setFindJgState(String findJgState) {
		this.findJgState = findJgState;
	}

	public String getFindJgShow() {
		return findJgShow;
	}

	public void setFindJgShow(String findJgShow) {
		this.findJgShow = findJgShow;
	}

	public String getDelIds() {
		return delIds;
	}

	public void setDelIds(String delIds) {
		this.delIds = delIds;
	}
	

	
}
